﻿using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Meishi.CURD
{
    /// <summary>
    /// CreatePage.xaml 的交互逻辑
    /// </summary>
    public partial class CreatePage : Page
    {

        Model.DBContext dBContext = new Model.DBContext();
        Model.Repository<Model.Menus> Rep = new Model.Repository<Model.Menus>();
        public CreatePage()
        {
            InitializeComponent();
            GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<Model.MenuClass>(this,"MenuClassUpdate" , UpdateMenusClass);
            vm = this.DataContext as ViewModel.MenuViewModel;

            vm.CurrentMenu = new Model.Menus() { Id=dBContext.DB.Queryable<Model.Menus>().Max(m=>m.Id) +1};
        }


        ViewModel.MenuViewModel vm;
        private void UpdateMenusClass( Model.MenuClass mc)
        {

        }
        private void btn_NewClass_Click(object sender, RoutedEventArgs e)
        {
            HandyControl.Controls.Dialog.Show( new View.Meishi.CURD.NewMenuClassUserControl());
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {

        }

        private async void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty( vm.CurrentMenu.Name))
            {
                Growl.SuccessGlobal("请输入菜品名称！");
                return;
            }
             


            if (vm.CurrentMenu !=null)
            {
                try
                {
                    await Rep.InsertAsync(vm.CurrentMenu);
                    var list = await Rep.GetListAsync();
                    vm.MenusList = new System.Collections.ObjectModel.ObservableCollection<Model.Menus>(list);
                    Growl.SuccessGlobal("菜品保存成功！");

                    vm.CurrentMenu = new Model.Menus();
                }
                catch (Exception ex)
                {

                    Growl.ErrorGlobal(ex.Message);
                }
              
               
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if(cmb_MenuClass.SelectedValue ==null)
            {
                return;
            }
            if ((int)cmb_MenuClass.SelectedValue <= 0)
            {
                Growl.WarningGlobal("请选择类别");
            }
            else
            {
                if(vm==null)
                {
                    return;
                }
                //vm.CurrentMenu.Id = dBContext.DB.Queryable<Model.Menus>().Where(m=>m.ClassId==).;
               // System.Windows.MessageBox.Show(vm.CurrentMenu.Id.ToString());
            }
           
        }
    }
}
